package org.omich.loaves.gui.view;

import org.omich.loaves.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

public class Person extends View
{
	private long mTail = -1;
	private long mWeight = -1;

	private int mLoafRes = R.drawable.loaf_middle;
	private Bitmap[] mFaceBitmaps = new Bitmap[3];
	private Bitmap mLoafBitmap;
	private Bitmap mLoafEndBitmap;
	private Shader mLoafShader;
	private Shader mLoafEndShader;
	
	private Paint mPaint;
	private int mHomerCounter;
	
	private float mHomerScale;
	private float mHomerOffsetX;
	private float mOffsetY;
	
	public Person (Context context)
	{
		super(context);
		create(context);
	}

	public Person (Context context, AttributeSet attrs) 
	{
		super(context, attrs);
		create(context);
	}

	private void create(Context context)
	{		
		mPaint = new Paint(Paint.DITHER_FLAG);
		mPaint.setAntiAlias(true);
		mPaint.setFilterBitmap(true);
		
		Drawable fd = context.getResources().getDrawable(R.drawable.homer1);
		mFaceBitmaps[0] = ((BitmapDrawable)fd).getBitmap();
		fd = context.getResources().getDrawable(R.drawable.homer2);
		mFaceBitmaps[1] = ((BitmapDrawable)fd).getBitmap();
		fd = context.getResources().getDrawable(R.drawable.homer3);
		mFaceBitmaps[2] = ((BitmapDrawable)fd).getBitmap();

		Drawable ld = context.getResources().getDrawable(mLoafRes);
		mLoafBitmap = ((BitmapDrawable)ld).getBitmap();
		//context.getResources().getDrawable(mLoafRes);
		ld = context.getResources().getDrawable(R.drawable.loaf_end);
		mLoafEndBitmap = ((BitmapDrawable)ld).getBitmap();
		
		mLoafShader = new BitmapShader(mLoafBitmap, TileMode.REPEAT, TileMode.CLAMP);
		mLoafEndShader = new BitmapShader(mLoafEndBitmap, TileMode.CLAMP, TileMode.CLAMP);
		
		
		mHomerScale = .7f;
		mHomerOffsetX = mFaceBitmaps[0].getWidth() * mHomerScale - 80;
		mOffsetY = mFaceBitmaps[0].getHeight() / 2 * mHomerScale + 15;
	}
	
	public void setName (String name)
	{
//		mNameView.setText(name);
	}
	
	public void setLoaf (int tail, int weight)
	{
		mTail = tail;
		mWeight = weight;
		invalidate();
	}
	
	@Override
	protected void onDraw (Canvas canvas)
	{
		float loafWidth = mWeight / 25f;
		float progressX = mTail / 25f;
		float offsetX = mHomerOffsetX + progressX - loafWidth;
		float scale = mHomerScale;
		
		Bitmap faceBitmap = mFaceBitmaps[mHomerCounter % 3];
		++mHomerCounter;

		if(mTail > 0)
		{
			Matrix m = new Matrix();
			m.setValues(new float[] {
					.2f, 0, offsetX, 
					0, .2f, mOffsetY, 
					0, 0, 1
				});
			mLoafShader.setLocalMatrix(m);
			mPaint.setShader(mLoafShader);
			RectF r = new RectF(Math.max(offsetX, mHomerOffsetX), mOffsetY, 
					offsetX + loafWidth, mOffsetY + 40);
			canvas.drawRect(r, mPaint);

			m.setValues(new float[] {
					.2f, 0, offsetX + loafWidth, 
					0, .2f, mOffsetY, 
					0, 0, 1
				});
			mLoafEndShader.setLocalMatrix(m);
			mPaint.setShader(mLoafEndShader);
			r = new RectF(offsetX + loafWidth, mOffsetY,
					offsetX + loafWidth + 60, mOffsetY + 50);
			canvas.drawRect(r, mPaint);
		}
		else
		{
			mPaint.setColor(Color.GREEN);
			mPaint.setTextSize(30);
			canvas.drawText("Take a Loaf !!!", mHomerOffsetX, 20, mPaint);
		}

		canvas.save();
		canvas.scale(scale, scale);
		mPaint.setShader(null);
		canvas.drawBitmap(faceBitmap, 0, 0, mPaint);
		canvas.restore();
	}
}
